import java.text.SimpleDateFormat;
import java.util.Date;

public class Test2_Runnable {
    public static void main(String[] args) {
        //创建一个进程绑定一个任务
        Runnable task = new ShowTimeTask();
        Thread t = new Thread();
        t.start();
        Runnable task2 = new ShowTimeTaskInner();
        Thread t2 = new Thread();
        t2.start();
        //匿名内部类的写法
        Thread t3 = new Thread(new Runnable() {
            private boolean flag;

            public void run() {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日 hh:mm:ss");
                Date d = null;
                while (!flag) {
                    d = new Date();
                    System.out.println(Thread.currentThread().getName() + "输出当前的时间" + sdf.format(d));
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        });
        t3.start();
        //语法四：因为Runnable接口上有@Functionalinterface 表示这个接口支持lamda语法
        Thread t4 = new Thread(() -> {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日 hh:mm:ss");
            Date d = null;
            while (true) {
                d = new Date();
                System.out.println(Thread.currentThread().getName() + "输出时间：" + sdf.format(d));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

        });

        t4.start();
    }

    static class ShowTimeTaskInner implements Runnable{
        private boolean flag;
        public void run(){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy年mm月dd日 hh:mm:ss");
            Date d=null;
            while(!flag){
                d=new Date();
                System.out.println(Thread.currentThread().getName()+"输出当前的时间"+sdf.format(d));
                try{
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}

class ShowTimeTask implements Runnable {
    private boolean flag;

    public void run() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日 hh:mm:ss");
        Date d = null;
        while (!flag) {
            d = new Date();
            System.out.println(Thread.currentThread().getName() + "输出当前的时间" + sdf.format(d));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
